package com.haulmont.asu.web.ui.project;

import com.haulmont.asu.entity.Project;
import com.haulmont.asu.web.ui.gantt.GanttDiagram;
import com.haulmont.cuba.gui.components.AbstractAction;
import com.haulmont.cuba.gui.components.AbstractEditor;
import com.haulmont.cuba.gui.components.Action;
import com.haulmont.cuba.gui.components.Component;
import org.jfree.ui.RefineryUtilities;

/**
 * Author: Sergey42
 * Date: 22.12.13 17:44
 */
public class ProjectEditor extends AbstractEditor<Project> {

    public static final int work = 1;
    public static final int step = 2;
    public static final int stage = 3;
    public static final int phase = 4;

    public static final int cancel = 0;

    int type = 0;

    public void gantt() {

        showOptionDialog(getMessage("gantTitle"), getMessage("gantMessage"), MessageType.CONFIRMATION,
                new Action[]{
                        new AbstractAction("gant.work") {
                            @Override
                            public void actionPerform(Component component) {
                                type = work;
                                build();
                            }
                        },
                        new AbstractAction("gant.step") {
                            @Override
                            public void actionPerform(Component component) {
                                type = step;
                                build();
                            }
                        },
                        new AbstractAction("gant.stage") {
                            @Override
                            public void actionPerform(Component component) {
                                type = stage;
                                build();
                            }
                        },
                        new AbstractAction("gant.phase") {
                            @Override
                            public void actionPerform(Component component) {
                                type = phase;
                                build();
                            }
                        },
                        new AbstractAction("gant.cancel") {
                            @Override
                            public void actionPerform(Component component) {
                                type = cancel;
                            }

                            @Override
                            public String getIcon() {
                                return "icons/cancel.png";
                            }
                        }
                });


    }

    protected void build() {
        final GanttDiagram demo = new GanttDiagram(type, getGanttCaption(), getItem().getName(), getItem());
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }

    protected String getGanttCaption() {
        return formatMessage("ganttCaption", getKeyForItem(), getItem().getName());
    }

    protected String getKeyForItem() {
        switch (type) {
            case work:
                return getMessage("work");
            case phase:
                return getMessage("phase");
            case step:
                return getMessage("step");
            case stage:
                return getMessage("stage");
            default:
                return getMessage("work");
        }
    }
}
